<?php
/**
 * Created by PhpStorm.
 * User: wangkxin@foxmail.com
 * Date: 2022/8/3
 * Time: 11:57
 */

namespace app\index\controller;

use app\api\server\Token;

define('KEY', '12f862d21dcfeafb57bckfrrt5yuiopf');

class TestEncrypt
{
    private $iv;
    /**
     * openssl 实现
     */
     public function testen($str){
         /**
          * openssl 实现
          */
         $iv= md5(time(). uniqid(),true);
         # 加密 md5->true 为16位md5
         $enStr = $strEncode= base64_encode(openssl_encrypt($str, 'AES-128-CBC',KEY, OPENSSL_RAW_DATA , $iv));   # AES-256-CBC
         echo $enStr;
         return $enStr;
        }

        /**
         * 解密
         */
        public function testde($strEncode)
        {
            /**
             * 解密
             */
            $iv= md5(time(). uniqid(),true);
            $destr = openssl_decrypt(base64_decode($strEncode), 'AES-128-CBC', KEY, OPENSSL_RAW_DATA, $iv);
            echo $destr;
            return $destr;
        }


        public function useEncrypt()
        {
            // phpinfo();

            $config = array(
                "digest_alg"    => "sha512",
                "private_key_bits" => 4096,           //字节数  512 1024 2048  4096 等 ,不能加引号，此处长度与加密的字符串长度有关系，可以自己测试一下
                "private_key_type" => OPENSSL_KEYTYPE_RSA,   //加密类型
            );
            $res =    openssl_pkey_new($config);
            //提取私钥
            openssl_pkey_export($res, $private_key);

            //生成公钥
            $public_key = openssl_pkey_get_details($res);
            // var_dump($public_key);

            $public_key=$public_key["key"];

            //显示数据
            // var_dump($private_key);    //私钥
            echo '<hr>';
            // var_dump($public_key);     //公钥

            //要加密的数据
            $data = "http://www.cnblogs.com/wt645631686/";
            echo '要加密的数据：'.$data."\r\n";
            echo '<hr>';
            //私钥加密后的数据
            openssl_private_encrypt($data,$encrypted,$private_key);

            //加密后的内容通常含有特殊字符，需要base64编码转换下
            $encrypted = base64_encode($encrypted);
            echo "私钥加密后的数据:".$encrypted."\r\n";
            echo '<hr>';
            //公钥解密
            openssl_public_decrypt(base64_decode($encrypted), $decrypted, $public_key);
            echo "公钥解密后的数据:".$decrypted,"\r\n";
            echo '<hr>';

            //----相反操作。公钥加密
            openssl_public_encrypt($data, $encrypted, $public_key);
            $encrypted = base64_encode($encrypted);
            echo "公钥加密后的数据:".$encrypted."\r\n";
            echo '<hr>';

            openssl_private_decrypt(base64_decode($encrypted), $decrypted, $private_key);//私钥解密
            echo "私钥解密后的数据:".$decrypted."n";
            echo '<hr>';

            return '公钥加密测试';
            // 加密
            // $new = new TestEncrypt();
            // $str = 'Hello,world';
            // $string = $new->testen($str);
            // echo '<hr>';

            // $s = $new->testde($string);
            // $data = 'Hello,world';
            // $data2 = openssl_encrypt($data, 'des-ecb', 'dfsf3s@!Q$fse8');
            // echo $data2;
            // echo "<hr>";
            // $data = openssl_decrypt($data2, 'des-ecb', 'dfsf3s@!Q$fse8');
            // echo $data;

        }






}

// / E80A6DD061E8CB53ABED62C7DCCEEEFA
// // print_r($string);
// // echo '<hr>';
//
// // 解密
// // $string =$new->testde($string); // richie
// // print_r($string);